package com.culturalCenter.dataCenter.Utils;

import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Message {
	/*static String x="{\"code\":\"测试\"}";
	static String 	msg=java.net.URLEncoder.encode(x);

	static String  mob = "13422240093";//电话号码
	private static final String addr="http://api.sms.cn/sms/";
	private static final String userid = "ctp20808";
	private static final String pwd = "db3fa56747e67647d563d4c9aa2962ba";*/

	public static void main(String[] args) throws Exception {
		Message m = new Message();
//		m.send(msg,mob);
	}
	public int send(String msg,String mobile) throws Exception{
		if (StringUtils.isEmpty(msg)){
			msg="测试";
		}
		String x="{\"code\":\""+msg+"\"}";
		String content=java.net.URLEncoder.encode(x);

//		String  mob = "13422240093";//电话号码
		String addr="http://api.sms.cn/sms/";
		String userid = "ctp20808";
		String pwd = "db3fa56747e67647d563d4c9aa2962ba";
		System.out.println("xxxxxxxxxxxxxxxxxxxxxxx");
		int nRet = 0;

		String straddr = addr +"?ac=send&uid="+userid+"&pwd="+pwd+"&template=100006&mobile="+mobile+"&encode=utf8&content="+content;
		StringBuffer sb = new StringBuffer(straddr);


		//
		//sb.append("&content="+URLEncoder.encode(msg, "UTF-8"));

		//
		URL url = new URL( sb.toString());

		//URL  url = new URL(URLEncoder.encode(sb.toString(),"UTF-8") );


		//
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();

		//
		connection.setRequestMethod("POST");

		//
		BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

		//
		String inputline = in.readLine();

		//
		System.out.println(inputline);

		//if(! inputline.equals("100"))
		//   nRet = 1;

		return nRet;
	}
}
